package com.ruoyi.cs.base.customer.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.base.customer.domain.CustomerContact;
import com.ruoyi.cs.base.customer.domain.CustomerInfo;
import com.ruoyi.cs.base.customer.mapper.CustomerInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户管理
 * 业务执行层
 * @author Drj 2021/6/28
 */
@Service
public class CustomerInfoServiceImpl implements ICustomerInfoService {
    @Autowired
    private CustomerInfoMapper customerInfoMapper;



    /**
     * 客户信息查询
     *
     * @param customerInfo
     * @return
     */
    @Override
    public List<CustomerInfo> selectCustomerInfoList(CustomerInfo customerInfo) {
        List<CustomerInfo> customerInfos = customerInfoMapper.selectCustomerInfoList(customerInfo);
        for (int i = 0; i < customerInfos.size(); i++) {
            if (StringUtils.isNotNull(customerInfoMapper.selectCustomerContactListById(customerInfos.get(i).getCustomerInfoId()))){
                //存在，则存入客户信息对应联系人信息
                customerInfos.get(i).setCustomerContacts(customerInfoMapper.selectCustomerContactListById(customerInfos.get(i).getCustomerInfoId()));
            }
        }
        return customerInfos;
    }

    /**
     * 按编号查询
     *
     * @param customerInfoId 客户信息编号
     * @return
     */
    @Override
    public CustomerInfo selectCustomerInfoById(String customerInfoId) {
        return customerInfoMapper.selectCustomerInfoById(customerInfoId);
    }

    /**
     * 客户信息新增
     *
     * @param customerInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCustomerInfo(CustomerInfo customerInfo) throws Exception{
        if (StringUtils.isNotNull(customerInfoMapper.selectCustomerInfoById(customerInfo.getCustomerInfoId()))){
            //存在
            return  0;
        }
        //不存在
        customerInfoMapper.insertCustomerInfo(customerInfo);//客户信息据
        List<CustomerContact> customerContacts = customerInfo.getCustomerContacts();//客户信息联系人信息
        for (int i = 0; i < customerContacts.size(); i++) {
            customerInfoMapper.insertCustomerContact(customerContacts.get(i));//客户信息联系人信息
        }
        return 1;

    }

    /**
     * 客户信息更新
     *
     * @param customerInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCustomerInfo(CustomerInfo customerInfo) throws Exception{
        if (!StringUtils.isNotNull(customerInfoMapper.selectCustomerInfoById(customerInfo.getCustomerInfoId()))){
            //不存在
            return  0;
        }
        //存在客户信息单据
        customerInfoMapper.updateCustomerInfo(customerInfo);//客户信息单据
        List<CustomerContact> customerContacts1 = customerInfoMapper.selectCustomerContactListById(customerInfo.getCustomerInfoId());
        List<CustomerContact> customerContacts = customerInfo.getCustomerContacts();//客户信息联系人信息
        for (int i = 0; i < customerContacts.size(); i++) {
            CustomerContact customerContact = customerInfoMapper.selectCustomerContactById(customerContacts.get(i).getCustomerContactId());
            if (StringUtils.isNotNull(customerContact)){
                //存在客户联系人详细
                for (int j = 0; j < customerContacts1.size(); j++) {
                    if (customerContacts1.get(j).getCustomerContactId().equals(customerContact.getCustomerContactId())){
                        customerContacts1.remove(j);//移除新旧都有的部分
                        break;
                    }
                }//更新
                customerInfoMapper.updateCustomerContact(customerContacts.get(i));
            }else{//新增
                customerInfoMapper.insertCustomerContact(customerContacts.get(i));
            }
        }
        for (int i = 0; i < customerContacts1.size(); i++) {
            //删除新旧重叠以外的部分
            customerInfoMapper.deleteCustomerContactById(customerContacts1.get(i).getCustomerContactId());
        }
        return 1;
    }

    /**
     * 更新客户信息删除标志位
     *
     * @param customerInfo
     * @return
     */
    @Override
    public int updateCustomerInfoDeleteFlag(CustomerInfo customerInfo) {
        return customerInfoMapper.updateCustomerInfoDeleteFlag(customerInfo);
    }


}
